package de.dopichaj.labrador.search.hit;

import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import de.dopichaj.labrador.search.QueryCategory;


/**
 * Double Hashtables for fast retrieval of parent hits.
 * @author Benedikt Eger
 */
public class HitUpdateTable {
    
    private static final Logger log = Logger.getLogger(HitUpdateTable.class);
    
    /**
     * A map from XML file path to a map from XPath to the corresponding hit.
     */
    private final Map<String, CategorizedHit> xPathMap
        = new HashMap<String, CategorizedHit>();
    
    /**
     * Constructor.
     * @param hits A HitList with hits.
     */
    public HitUpdateTable(Collection<CategorizedHit> hits) {
        if (hits.size() != 0) {
            File file = hits.iterator().next().getFile();
            for (CategorizedHit hit : hits) {
                assert file.equals(hit.getFile()) :
                    "Adding hits from different documents to same HitUpdateTable.";
                xPathMap.put(hit.getXPath(), hit);
            }
        }
    }
    
    /**
     * Updates scores from the current hit up its XPath.
     * @param category the category (as used in the query).
     */
    public void updateScoresAlongPath(
        CategorizedHit hit,
        double coherenceValue,
        QueryCategory category,
        Set<CategorizedHit> processedHits) {
        
        if (hit.getCategoryScore(category) != 0 && !processedHits.contains(hit)) {
            
            if (log.isDebugEnabled())
                log.debug("Updating score of " + hit);
            
            hit.setCategoryScore(category,
                (float)(hit.getCategoryScore(category) * coherenceValue));
            processedHits.add(hit);
        }
        CategorizedHit parentHit = getParentHit(hit);
        if (parentHit != null) {
            updateScoresAlongPath(parentHit, coherenceValue, category, processedHits);
        }
    }
    
    /**
     * Gets the parent Hit from a Hit.
     * @param hit The Hit.
     * @return Its parent.
     */
    private CategorizedHit getParentHit(CategorizedHit hit) {
        String parentXPath = hit.getEnclosingXPath();
        CategorizedHit parentHit = xPathMap.get(parentXPath);
        return parentHit;
    }        
}
/*
Copyright (c) 2005 Benedikt Eger
Copyright (c) 2005-2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/